package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.InventorySummaryQueryDto;
import com.one.group.model.entity.InventorySummary;
import com.one.group.model.vo.InventorySummaryListVo;
import com.one.group.result.Result;
import com.one.group.service.InventorySummaryService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("inventorySummary")
@Tag(name = "库存管理接口")
public class InventorySummaryController {
    @Autowired
    InventorySummaryService inventorySummaryService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @PostMapping("list")
    @Operation(summary = "查询库存列表")
    public Result list(@RequestBody InventorySummaryQueryDto inventorySummaryQueryDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        inventorySummaryQueryDto.setTenantId(tenantId);
//        1.准备分页条件
        PageHelper.startPage(inventorySummaryQueryDto.getPage(),inventorySummaryQueryDto.getLimit());
//        2.链式编程准备查询条件

        List<InventorySummaryListVo> inventorySummaryListVos = inventorySummaryService.findInventorySummaryByQueryDto(inventorySummaryQueryDto);

//        3.获取分页信息
        PageInfo<InventorySummaryListVo> pageInfo =new PageInfo<>(inventorySummaryListVos);
//        4.返回分页数据
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询库存信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(inventorySummaryService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有库存信息")
    public Result findAll() {
        return ResultUtil.success(inventorySummaryService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除库存信息")
    public Result delete(@RequestParam Long id){
        if (inventorySummaryService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加库存信息")
    public Result add(@RequestBody InventorySummary inventorySummary){
        if (inventorySummaryService.save(inventorySummary)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改库存信息")
    public Result update(@RequestBody InventorySummary inventorySummary){
        if (inventorySummaryService.updateById(inventorySummary)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除库存信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (inventorySummaryService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
